﻿//***************************************************
//
//       File Name:    ChaoKuBusRotate.cs
//
//       Author:   王文军    Email:  546527816@qq.com
//
//
//***************************************************

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ChaoKuBusRotate : MonoBehaviour
{
    ////模型初始角度
    //private float x = -90;
    //private float y = 0;
    //private float z = 0;
    //private float distance = 0;
    //private float lastDist = 0;
    //private float curDist = 0;
    ////触摸旋转速度
    //private float xSpeed = 2;

    ////缩放最大最小值
    //private float minimumDistance = 0;
    //private float maximumDistance = 140;
    //private Touch touch;
    ////旋转角度范围
    //private float eulerAngles1=30;
    //private float eulerAngles2=155;
    //private float eulerAngles3=210;
    //private float eulerAngles4=335;
    ////拖动范围
    //private float minPosX = -900f;
    //private float maxPosX = 1000f;
    //private float minPosY = -350f;
    //private float maxPosY = 300f;
    
    private Vector3 myLocalPostion;
    private Vector3 myLocalScale;
    private Quaternion myRotation;

    private Camera uiCamera;
    public VehicleInfo vehicleInfo;

    //车轮
    private Transform chelunOne;
    private Transform chelunTwo;
    private Transform chelunThree;
    private Transform chelunFour;
    private Transform chelunFive;
    private Transform chelunSix;
    private Transform cheShen;
    //各种组件是否显示
    private bool isNewAppearanceShow = true;
    private bool isNewPowerShow = true;
    private bool isNewMaterialShow = true;
    private bool isNewEngineShow = true;
    private bool isNewDirectionShow = true;
    private bool isRotate = true;
    private bool isZoom = false;
    public bool IsZoom
    {
        set { isZoom = value; }
    }
    public bool IsNewAppearanceShow
    {
        set { isNewAppearanceShow = value; }
    }
    public bool IsNewPowerShow
    {
        set { isNewPowerShow = value; }
    }
    public bool IsNewMaterialShow
    {
        set { isNewMaterialShow = value; }
    }
    public bool IsNewEngineShow
    {
        set { isNewEngineShow = value; }
    }
    public bool IsNewDirectionShow
    {
        set { isNewDirectionShow = value; }
    }
    public bool IsRotate
    {
        set { isRotate = value; }
    }



    void Awake()
    {
        chelunOne = transform.Find("chaokubashiChild/chelunzi00/baked_chelunzi");
        chelunTwo = transform.Find("chaokubashiChild/chelunzi01/baked_chelunzi001");
        chelunThree = transform.Find("chaokubashiChild/chelunzi02/baked_chelunzi002");
        chelunFour = transform.Find("chaokubashiChild/chelunzi03/baked_chelunzi003");
        chelunFive = transform.Find("chaokubashiChild/chelunzi04/baked_chelunzi004");
        chelunSix = transform.Find("chaokubashiChild/chelunzi05/baked_chelunzi005");
        cheShen = transform.Find("chaokubashiChild");
        myLocalScale = cheShen.transform.localScale;
        myLocalPostion = cheShen.transform.localPosition;
        myRotation = cheShen.transform.localRotation;
        uiCamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent<Camera>();
}

    public void SetCheLunShow()
    {
        if (!isNewPowerShow)
        {
            chelunOne.gameObject.SetActive(false);
            chelunTwo.gameObject.SetActive(false);
            chelunThree.gameObject.SetActive(false);
            chelunFour.gameObject.SetActive(false);
            chelunFive.gameObject.SetActive(false);
            chelunSix.gameObject.SetActive(false);
        }
        else if (isNewPowerShow)
        {
            chelunOne.gameObject.SetActive(true);
            chelunTwo.gameObject.SetActive(true);
            chelunThree.gameObject.SetActive(true);
            chelunFour.gameObject.SetActive(true);
            chelunFive.gameObject.SetActive(true);
            chelunSix.gameObject.SetActive(true);
        }
    }
    /// <summary>
    /// 旋转车轮身
    /// </summary>
    void OnRotate()
      {
        if (isRotate)
            cheShen.Rotate(Vector3.up, 15 * Time.deltaTime);
        if (isNewPowerShow)
        {
            chelunOne.Rotate(Vector3.left, 15);
            chelunTwo.Rotate(Vector3.left, 15);
            chelunThree.Rotate(Vector3.left, 15);
            chelunFour.Rotate(Vector3.left, 15);
            chelunFive.Rotate(Vector3.left, 15);
            chelunSix.Rotate(Vector3.left, 15);
        }
    }

    ///// <summary>
    ///// 触摸缩放旋转物体
    ///// </summary>
    //void OnTouchMoudle()
    //{
    //    if (!isZoom) return;
    //    RaycastHit hit;
    //    //if (Input.GetKeyDown(KeyCode.Escape))
    //    //    Application.Quit();
    //    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
    //    {
    //        Ray ray = uiCamera.ScreenPointToRay(Input.GetTouch(0).position);
    //        if (Physics.Raycast(ray, out hit))
    //        {
    //            touch = Input.GetTouch(0);
    //            if (hit.collider.tag == StringManager.MoudleOBJTag)
    //            {
    //                //在一定范围里旋转
    //                if (cheShen.eulerAngles.y < eulerAngles2 && cheShen.eulerAngles.y > eulerAngles1)
    //                    y += touch.deltaPosition.y* xSpeed;//  * 0.02f* xSpeed
    //                if (cheShen.eulerAngles.y < eulerAngles4 && cheShen.eulerAngles.y > eulerAngles3)
    //                    y -= touch.deltaPosition.y* xSpeed;//  * 0.02f* xSpeed
    //                x -= touch.deltaPosition.x;//  * 0.02f* ySpeed 
    //                Vector3 vt=new Vector3(touch.deltaPosition.x * 0.02f, touch.deltaPosition.y * 0.02f,0);
                   
    //                if (distance > 30)
    //                {
    //                    //触摸拖动
    //                    cheShen.position += vt;
    //                    Vector3 cheShenPos = cheShen.transform.localPosition;
    //                    cheShenPos.x = Mathf.Clamp(cheShenPos.x, minPosX, maxPosX);
    //                    cheShenPos.y = Mathf.Clamp(cheShenPos.y, minPosY, maxPosY);
    //                    cheShen.localPosition = cheShenPos;
    //                }
    //                Quaternion rotation = Quaternion.Euler(0, x, y);
    //                if (distance <= 30)
    //                    cheShen.rotation = rotation;
    //            }
    //        }
    //    }
    //    if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
    //    {

    //        var touch1 = Input.GetTouch(0);
    //        var touch2 = Input.GetTouch(1);
    //        curDist = Vector2.Distance(touch1.position, touch2.position);
    //        if (curDist > lastDist)
    //            distance += Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition); //* pinchSpeed / 10;
    //        else {
    //            distance -= Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition); // * pinchSpeed / 10;
    //        }
    //        lastDist = curDist;
    //    }
    //    if (distance <= minimumDistance)
    //        distance = minimumDistance;
    //    if (distance >= maximumDistance)
    //        distance = maximumDistance;
    //    Vector3 position = new Vector3(distance, distance, distance);
    //    //缩放
    //    cheShen.localScale = position + myLocalScale;
    //}

    /// <summary>
    /// 复位
    /// </summary>
    public void ResetCheShen()
    {
        TouchMoudel.distance = 0;
        TouchMoudel.lastDist = 0;
        TouchMoudel.curDist = 0;
        cheShen.rotation = myRotation;
        cheShen.localPosition = myLocalPostion;
        cheShen.localScale = myLocalScale;
    }

    void Update()
    {
        OnRotate();

        //OnTouchMoudle();

        if (!isZoom) return;
        TouchMoudel.TouchManager(uiCamera,cheShen,myLocalScale,vehicleInfo);
    }

    //void OnGUI()
    //{
    //    //Simple output to display the distance from the center
    //    GUI.Label(new Rect(0, 0, Screen.width, Screen.height), distance.ToString());
    //}


}
